﻿using King.Box.AdminWeb.Utils;
using King.Box.Models.Db.Sys;
using King.Box.Models.Util;
using King.Box.Models.View.Sys;
using King.Box.Utils.Global;
using King.Box.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace King.Box.AdminWeb.Logic
{
    public class HomeService
    {
        public ResponseData<Sys_UserData> AutoLogin()
        {
            var loginData = ApiClientUtil.Post<ResponseData<Sys_UserData>>($"home/admin/autologin/{AuthorizationCustomerService.UserId}");
            //保存登录信息
            if (loginData != null && loginData.Result)
                HttpContext.Current.Session[GlobalConst.Session_User] = loginData.Data;
            
            return loginData;
        }

        public ResponseData<Sys_UserData> Login(string userName, string pwd)
        {
            var loginData = ApiClientUtil.Post<ResponseData<Sys_UserData>>($"home/admin/login/{userName}/{pwd}");

            //保存登录信息
            if (loginData != null && loginData.Result)
            {
                AuthorizationCustomerService.UserId = loginData.Data.Id;

                HttpContext.Current.Session[GlobalConst.Session_User] = loginData.Data;
            }

            return loginData;
        }

        public void LogOut()
        {
            CookieUtil.ClearCookie(GlobalConst.Cookie_User);
        }
    }
}